package me.zhengjie.inter.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.*;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.QyService;
import me.zhengjie.business.service.YbryService;
import me.zhengjie.business.service.dto.BisQyRowDto;
import me.zhengjie.business.service.dto.SnryDto;
import me.zhengjie.business.service.dto.SnryQueryCriteria;
import me.zhengjie.inter.service.MpSnryService;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiahongbo
 * @date 2022-07-10
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "移动端失能人员管理")
@RequestMapping("/mp/snry")
public class MpSnryController {

    private final MpSnryService mpSnryService;
    private final YbryService ybryService;
    private final QyService qyService;
    private final GsglService gsglService;

    private final ObjectMapper mapper = new ObjectMapper();

    @GetMapping
    @ApiOperation("移动端查询失能人员")
    public ResponseEntity<Object> querySnry(SnryQueryCriteria criteria) {
        criteria.setBisSnryUserId(SecurityUtils.getCurrentUserId());
        Map<String,Object> rsMap = PageUtil.toMpContent(mpSnryService.queryAll(criteria));
        log.info("移动端[/mp/snry - get] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.OK);
    }

    @GetMapping(value = "/querySnryPage")
    @ApiOperation("移动端查询失能人员")
    public ResponseEntity<Object> querySnryPage(SnryQueryCriteria criteria, Pageable pageable) throws JsonProcessingException {
        criteria.setBisSnryUserId(SecurityUtils.getCurrentUserId());
        Long userid = SecurityUtils.getCurrentUserId();
        Map<String,Object> rsMap = PageUtil.toMpContent(mpSnryService.queryAll(criteria,pageable));
        log.info("移动端[/mp/snry - get] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.OK);
    }

    @PostMapping
    @Log("移动端新增失能人员")
    @ApiOperation("移动端新增失能人员")
    public ResponseEntity<Object> createSnry(@Validated @RequestBody Snry resources, @RequestParam String ids) {
        Map<String,Object> rsMap = PageUtil.toMpContent(mpSnryService.create(resources, ids));
        log.info("移动端[/mp/snry - post] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.CREATED);
    }

    @PutMapping
    @Log("移动端修改失能人员")
    @ApiOperation("移动端修改失能人员")
    public ResponseEntity<Object> updateSnry(@Validated @RequestBody Snry resources, @RequestParam String ids){
        Map<String,Object> rsMap = PageUtil.toMpContent(mpSnryService.update(resources, ids));
        log.info("移动端[/mp/snry - put] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.CREATED);
    }

    @DeleteMapping
    @Log("移动端删除失能人员")
    @ApiOperation("移动端删除失能人员")
    public ResponseEntity<Object> deleteSnry(@RequestBody String[] ids) {
        mpSnryService.deleteAll(ids);
        Map<String,Object> rsMap = PageUtil.toMpContent(1);
        log.info("移动端[/mp/snry - delete] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.OK);
    }

    @GetMapping(value = "/querySnryCbInfo")
    @ApiOperation("查询失能人员参保信息")
    public ResponseEntity<Object> querySnryCbInfo(String sfzh) {
        Map<String,Object> rsMap = PageUtil.toMpContent(ybryService.queryCareinsuInfo(sfzh));
        log.info("移动端[/mp/snry - get] 返回值 {}", rsMap);
        return new ResponseEntity<>(rsMap, HttpStatus.OK);
    }

    @GetMapping(value = "/queryQyJd")
    @ApiOperation("移动端查询长春市内区域街道")
    public ResponseEntity<Object> queryQyJd(SnryQueryCriteria criteria) {
        JSONArray objects = mpSnryService.queryQyJd("03");
        return new ResponseEntity<>(PageUtil.toMpContent(objects), HttpStatus.OK);
    }

    /**
     * 一个用户，最多新增五个患者
     * @return
     */
    @GetMapping(value = "/checkAddSnry")
    @ApiOperation("移动端新增老人校验")
    public ResponseEntity<Object> checkAddSnry() {
        Long userid = SecurityUtils.getCurrentUserId();
        return new ResponseEntity<>(PageUtil.toMpContent(mpSnryService.checkCreate(userid)), HttpStatus.OK);
    }
}
